package com.hkbigdata.datagovernance.governance.assess.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hkbigdata.datagovernance.governance.assess.Assessor;
import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import com.hkbigdata.datagovernance.governance.bean.GovernanceMetric;
import com.hkbigdata.datagovernance.meta.bean.TableMetaInfo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component("LIFECYCLE")
//未设定周期类型的 给 0分
//周期类型为永久、拉链表 则给10分
//周期类型为日分区 :
//无分区信息的给0分
//        没设生命周期给0分
//周期长度超过建议周期天数{days}，则给 （建议周期天数  / 设置周期天数 ）*10

public class LifecycleAssessor extends Assessor {
    @Override
    public  void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam ) {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        JSONObject paramJson = JSON.parseObject(governanceMetric.getMetricParamsJson());
        Integer lifecycleSuggestDays = paramJson.getInteger("days");

        if(tableMetaInfo.getTableMetaInfoExtra().getLifecycleType()==null||tableMetaInfo.getTableMetaInfoExtra().getLifecycleType().equals("UNSET")){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(0));
            governanceAssessDetail.setAssessProblem("未设置生命周期类型");
            governanceAssessDetail.setGovernanceUrl(governanceMetric.getGovernanceUrl()+"/"+tableMetaInfo.getId());

        }else  if(tableMetaInfo.getTableMetaInfoExtra().getLifecycleType().equals("NORMAL")){
            if(tableMetaInfo.getPartitionColNameJson()!=null){
                List<JSONObject> partitionColList = JSON.parseArray(tableMetaInfo.getPartitionColNameJson(), JSONObject.class);
                if(partitionColList==null||partitionColList.size()==0){
                    governanceAssessDetail.setAssessScore(BigDecimal.valueOf(0));
                    governanceAssessDetail.setAssessProblem("缺少分区信息");
                }else if(tableMetaInfo.getTableMetaInfoExtra()==null||tableMetaInfo.getTableMetaInfoExtra().getLifecycleDays()==0L){
                    governanceAssessDetail.setAssessScore(BigDecimal.valueOf(0));
                    governanceAssessDetail.setGovernanceUrl(governanceMetric.getGovernanceUrl()  +tableMetaInfo.getId());
                    governanceAssessDetail.setAssessProblem("缺少生命周期");
                } else if(tableMetaInfo.getTableMetaInfoExtra().getLifecycleDays()>lifecycleSuggestDays){

                    long score = lifecycleSuggestDays * 10 / tableMetaInfo.getTableMetaInfoExtra().getLifecycleDays();
                    governanceAssessDetail.setAssessScore(BigDecimal.valueOf(score));
                    governanceAssessDetail.setAssessProblem("生命周期超过建议值");
                    governanceAssessDetail.setGovernanceUrl(governanceMetric.getGovernanceUrl() +tableMetaInfo.getId());
                }
            }
        }
    }
}
